package com.itheima.ydd.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.ydd.common.BusinessException;
import com.itheima.ydd.common.PageResult;
import com.itheima.ydd.common.ResultData;
import com.itheima.ydd.entity.YddArticle;
import com.itheima.ydd.entity.YddTag;
import com.itheima.ydd.service.IYddArticleService;
import com.itheima.ydd.service.IYddTagService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author author
 * @since 2022-05-22
 */
//标签管理
@RestController
@RequestMapping("/ydd_tag")
public class YddTagController {

    @Autowired
    private IYddTagService tagService;

    @Autowired
    private IYddArticleService articleService;

    /**
     * 分页展示所有标签列表
     *
     * @param tagname
     * @param page
     * @param limit
     * @return
     */
    @GetMapping("/list")
    public ResultData list(String tagname, int page, int limit) {
        Page<YddTag> pageInfo = new Page<>(page, limit);
        LambdaQueryWrapper<YddTag> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StringUtils.isNotEmpty(tagname), YddTag::getTagname, tagname);
        tagService.page(pageInfo, wrapper);
        //将数据封装为pageResult
        PageResult pageResult = PageResult.getResult(pageInfo);
        return ResultData.ok("",pageResult);
    }

    /**
     * 修改时数据回显
     *
     * @param yddTag
     * @return
     */
    @GetMapping("/rowInfo")
    public ResultData rowInfo(YddTag yddTag) {
        YddTag tag = tagService.getById(yddTag.getId());
        return ResultData.ok("",tag);
    }

    /**
     * 修改标签
     *
     * @param yddTag
     * @return
     */
    @PostMapping("/update")
    public ResultData update(@RequestBody YddTag yddTag) {
        tagService.updateById(yddTag);
        return ResultData.ok("","");
    }

    /**
     * 删除标签
     *
     * @param id
     * @return
     */
    @GetMapping("/del")
    public ResultData delte(String id) {
        YddTag yddTag = tagService.getById(id);
        if ("1".equals(yddTag.getDefaultData())) {
            throw new BusinessException("系统默认数据无法删除!");
        }
        LambdaQueryWrapper<YddArticle> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(YddArticle::getTags, yddTag.getTagname());
        int count = articleService.count(wrapper);
        if (count > 0) {
            throw new BusinessException("该标签下有关联的文章,无法删除!");
        }
        tagService.removeById(id);
        return ResultData.ok("","");

    }

    /**
     * 新增标签
     *
     * @param yddTag
     * @return
     */
    @PostMapping("/add")
    public ResultData save(@RequestBody YddTag yddTag) {
        tagService.save(yddTag);
        return ResultData.ok("","");
    }

    /**
     * 查询所有标签
     *
     * @return
     */
    @GetMapping("/all")
    public ResultData getAll() {
        List<YddTag> yddTags = tagService.list();
        return ResultData.ok("",yddTags);
    }
}
